import 'package:flutter/material.dart';
import 'package:loading_indicator/loading_indicator.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/size_extension.dart';
import 'package:gold/utils/xfg_color.dart';

import 'config.dart';

/// 描述: 根据PageState来决定展示的页面
/// 
/// @Author tangzhentao
///  @CreatedTime 20:37 2022/6/17
/// 

class AnimatedWidget extends StatelessWidget {
  /// 成功内容widget
  final Widget success;
  /// 正在加载widget
  final Widget? loading;
  /// 空数据widget
  final Widget? empty;
  /// 错误widget
  final Widget? error;
  /// 状态数据
  final PageState state;
  /// 是否需要动画处理
  final bool isAnimated;
  ///过度动画时间：毫秒，如：1000=1秒
  final Duration animationDuration;
  final Function()? onReTry;

  const AnimatedWidget({
    super.key,
    required this.state,
    required this.success,
    this.loading,
    this.empty,
    this.error,
    this.isAnimated = true,
    this.onReTry,
    this.animationDuration = const Duration(milliseconds: 250),
  });

  @override
  Widget build(BuildContext context) {
    return AnimatedSwitcher(
      duration: animationDuration,
      child: isAnimated ? AnimatedSize(
        duration: const Duration(milliseconds: 500),
        curve: Curves.easeOutCubic,
        child: _getCurrentWidget(),
      ) : _getCurrentWidget(),
    );
  }

  /// 获取当前状态要展示的Widget
  Widget _getCurrentWidget() {
    switch (state) {
      case PageState.success:
        return success;
      case PageState.loading:
        return loading ?? Container(
          height: 56.xr,
          alignment: Alignment.center,
          child: const LoadingIndicator(colors: [Colors.black26], indicatorType: Indicator.ballPulse),
        );
      case PageState.empty:
        return empty ?? emptyWidget(StateInfo('暂无内容，请刷新试试', Res.imagesPageEmpty), XfgColor.transparent);
      case PageState.error:
        return error ?? errorWidget(StateInfo('页面出错，请刷新试试', Res.imagesPageFailure), XfgColor.transparent);
    }
  }
}